package com.wxd.modules.function.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxd.annotation.DataFilter;
import com.wxd.dataSwitch.RabbitMqController;
import com.wxd.dataSwitch.RabbitProducer;
import com.wxd.modules.common.entity.*;
import com.wxd.modules.function.dao.SwitchDataChangeDao;
import com.wxd.modules.function.entity.EmpEntity;
import com.wxd.modules.function.entity.SwitchDataChange;
import com.wxd.modules.function.service.EmpService;
import com.wxd.modules.function.service.SwitchDataChangeService;
import com.wxd.modules.sys.entity.SysDeptEntity;
import com.wxd.modules.sys.entity.SysUserEntity;
import com.wxd.modules.sys.service.SysDeptService;
import com.wxd.utils.*;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 员工表
 */
@Service("switchDataChangeService")
public class SwitchDataChangeServiceImpl extends ServiceImpl<SwitchDataChangeDao, SwitchDataChange> implements SwitchDataChangeService {
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private EmpService empService;
    @Autowired
    private RabbitProducer rabbitProducer;

   /*在册*/
    @Override
    @DataFilter(subDept = true, user = false)
    public PageUtils queryPage(Map<String, Object> params) {
        String deptIdStr= (String) params.get("deptId");
        String dateStart=(String)params.get("dateStart");
        String dateEnd=(String)params.get("dateEnd");
        Long deptIdLong;
        if(StringUtils.isNotBlank(deptIdStr)) {
            //根据部门ID将部门下的所有人员查找出来
            List<Long> deptIdList = null;
            if (StringUtils.isNotBlank(deptIdStr)) {
                deptIdLong = Long.valueOf(deptIdStr);
                deptIdList = sysDeptService.getSubDeptIdList(deptIdLong);
                deptIdList.add(deptIdLong);
            }
            IPage<SwitchDataChange> page = this.page(
                    new Query<SwitchDataChange>().getPage(params),
                    new QueryWrapper<SwitchDataChange>()
                            .between(StringUtils.isNotBlank(dateStart), "date", dateStart, dateEnd)
                            .in(deptIdList != null, "dept_id", deptIdList)
                            .apply(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER))
            );
            for (SwitchDataChange empEntity : page.getRecords()) {
                EmpEntity emp = empService.getById(empEntity.getEmpId());
                SysDeptEntity sysdeptEntity = sysDeptService.getById(empEntity.getDeptId());
                empEntity.setDeptName(sysdeptEntity.getName());
                empEntity.setEmpName(emp.getEmpName());
            }
            return new PageUtils(page);
        }
        return new PageUtils();
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDataChangeEmpList(List<EmpEntity> empList) {
        //用户名
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        for(EmpEntity emp:empList)
        {
            /*更新人员数据,只能修改已经存在的人员信息*/
            empService.update(emp,new QueryWrapper<EmpEntity>().eq("id_card",emp.getIdCard()));
            /*记录变化的信息*/
            SwitchDataChange dataChange=new SwitchDataChange();
            dataChange.setEmpId(emp.getEmpId());
            dataChange.setIdCard(emp.getIdCard());
            dataChange.setDeptId(emp.getDeptId());
            dataChange.setOperator(username);
            dataChange.setDate(new Date());
            this.save(dataChange);
        }
    }

    @Override
    public R saveEmpToDb(SaveDataToDb saveDataToDb) {
        /*将数据保存到本地数据库*/
        ConnectDB connectDB=saveDataToDb.getConnectDB();
        Long[] empIdsList=saveDataToDb.getEmpIds();
        List<EmpEntity> empEntities = empService.queryEmp(empIdsList);
        String ip=connectDB.getIp();
        String dbType=connectDB.getDbType();
        String name=connectDB.getRoot();
        String password=connectDB.getPassword();
        return DbConnectUtil.connect(ip,dbType,name,password,empEntities);
    }

    @Override
    public R saveEmpToMq(Long[] empIds) {
        /*找到对应的消息队列，生成唯一的routerKey*/
        String routerKey = empService.queryParentDept(empIds[0]);
        List<EmpEntity> empEntities = empService.queryEmp(empIds);
        /*将数据保存到消息队列*/
        rabbitProducer.sendTopicTopic(routerKey,empEntities);
        return R.ok();
    }
}
